package com.yonyou.iuap.dispatch.common;

import com.yonyou.crm.sys.quartz.common.ContextUtil;
import com.yonyou.crm.sys.quartz.service.DispatchTaskBizService;
import com.yonyou.crm.sys.quartz.service.DispatchTaskLogBizService;
import com.yonyou.crm.sys.quartz.ventity.DispatchTaskLogVO;
import com.yonyou.iuap.dispatch.ITask;
import org.quartz.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

@DisallowConcurrentExecution
public class TaskClassJob implements Job {
	protected static Logger logger = LoggerFactory
			.getLogger(TaskClassJob.class);
	private Logger quartzLog = LoggerFactory.getLogger("quartzLog");
	public TaskClassJob() {
	}

	public void execute(JobExecutionContext context)
			throws JobExecutionException {
		JobDetail jobDetail = context.getJobDetail();
		String jobId = jobDetail.getKey().getName() + "."
				+ jobDetail.getKey().getGroup() + "--";
		JobDataMap jobDataMap = jobDetail.getJobDataMap();
		// 1、查询日志表中是否存在该任务的一些信息，根据id进行查询
		String logId = jobDetail.getKey().getName();
		DispatchTaskLogVO vo = insertTaskLogVO(jobDetail);
		String jobClassName = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		try {
			jobClassName = jobDataMap.getString("jobClassName");
			if ("".equals(jobClassName)) {
				quartzLog.error(jobId + "jobClassName is null, stop.");
				quartzLog.info(jobClassName+"任务执行更新开始时间："+sdf.format(new Date()));
				updateTaskLogVO(jobClassName + "在"+sdf.format(new Date())+" 执行失败，查询不到该任务!", vo);
				quartzLog.info(jobClassName+"任务执行更新结束时间："+sdf.format(new Date()));
			} else {
				ITask task = (ITask) Class.forName(jobClassName).newInstance();
				quartzLog.info(jobId + "task execute begain, jobClass="
						+ jobClassName);
				Map<String, Object> data = (Map) jobDataMap.get("jobDataMap");
				data.put("jobinfo", jobDetail);
				quartzLog.info(jobClassName+"任务执行启动开始时间："+sdf.format(new Date()));
				task.execute(data);
				quartzLog.info(jobClassName+"任务执行启动结束时间："+sdf.format(new Date()));
				quartzLog.info(jobId + "task execute over, jobClass="
						+ jobClassName);
				updateTaskLogVO(jobClassName + "在"+sdf.format(new Date())+" 执行成功!", vo);
			}
		} catch (Exception e) {
			updateTaskLogVO(jobClassName + "在"+sdf.format(new Date())+"  执行成功! 错误信息为：" +e.toString(), vo);
			quartzLog.error(jobId + "execute task error", e);
		}
	}

	public void updateTaskLogVO(String log, DispatchTaskLogVO vo) {

		vo.setEndtime(new Date());
		Integer intflag = Integer.valueOf(1);

		vo.setContent(log.getBytes());
		byte[] loo = log.getBytes();
		vo.setContent(loo);

		getTaskLogService().updateTaskLog(vo);
	}

	public DispatchTaskLogVO insertTaskLogVO(JobDetail jobDetail) {
		DispatchTaskLogVO taskLog = new DispatchTaskLogVO();
		taskLog.setStarttime(new Date());
		taskLog.setTaskid(jobDetail.getKey().getName());
		taskLog.setId(jobDetail.getKey().getName());
		taskLog.setEndtime(new Date());
		DispatchTaskLogVO taskVO = getTaskLogService().getVOById(
				jobDetail.getKey().getName());
		if (taskVO.getId() == null) {
//			getTaskLogService().saveTaskLog(taskLog);
			taskLog.setVisible(Integer.valueOf(0));
		} else {
			taskLog.setVisible(Integer.valueOf(1));
		}
		return taskLog;
	}

	private DispatchTaskLogBizService getTaskLogService() {
		return (DispatchTaskLogBizService) ContextUtil
				.getBean("DispatchTaskLogBizService");
	}

	private DispatchTaskBizService getTaskService() {
		return (DispatchTaskBizService) ContextUtil
				.getBean("DispatchTaskBizService");
	}

}
